package com.feige.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.constant.Constants;
import com.feige.pojo.Course;
import com.feige.pojo.User;
import com.feige.pojo.vo.CourseVo;
import com.feige.result.AjaxResult;
import com.feige.service.CourseService;
import com.feige.utils.inventcode.ShareCodeUtil;
import com.feige.utils.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Api(tags = "课程表控制器")
@RestController
@RequestMapping("/course/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @Autowired
    private RedisUtils redisUtils;



    @ApiOperation(value = "查询我教的课程")
    @GetMapping("/get/teach/{pageNum}/{userId}")
    public AjaxResult getMyTeachCourse(@PathVariable String userId, @PathVariable Integer pageNum){
        IPage<Course> page = new Page<>(pageNum, Constants.PAGE_SIZE);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.select("id","name","join_count")
                .eq("user_id",userId)
                .orderByDesc("gmt_create");
        IPage<Course> coursePage = courseService.page(page, wrapper);
        List<Course> records = coursePage.getRecords();
        long total = coursePage.getTotal();
        AjaxResult success = AjaxResult.success(records);
        success.put("total",total);
        return success;
    }


    @ApiOperation(value = "查询我听的课程")
    @GetMapping("/get/attend/{pageNum}/{userId}")
    public AjaxResult getMyAttendCourse(@PathVariable String userId, @PathVariable Integer pageNum){
        List<CourseVo> myAttendCourse = courseService.getMyAttendCourse(pageNum, Constants.PAGE_SIZE, userId);
        long total = courseService.getMyAttendCourseCount(userId);

        AjaxResult success = AjaxResult.success(myAttendCourse);
        success.put("total",total);
        return success;
    }

    @ApiOperation("通过ID查询课程")
    @GetMapping("/get/{id}")
    public AjaxResult getCourseById(@PathVariable String id) {
        CourseVo course = courseService.getCourseVoById(id);
        if (ObjectUtil.isNotEmpty(course)){
            course.setInventCode(ShareCodeUtil.toSerialCode(course.getInventCode()));
            return AjaxResult.success(course);
        }else {
            return AjaxResult.error(5005,"未查询到数据！");
        }
    }

    @ApiOperation("通过课程ID查询课程的创建者")
    @GetMapping("/create/user/{courseId}")
    public AjaxResult getUserByCourseId(@PathVariable String courseId) {
        User user = courseService.getUserByCourseId(courseId);
        return AjaxResult.success(user);
    }

    @ApiOperation("通过邀请码查询课程")
    @GetMapping("/get/invest/{investCode}")
    public AjaxResult getCourseByInvestCode(@PathVariable String investCode) {
        CourseVo course = courseService.getCourseVoByInvestCode(investCode);
        if (ObjectUtil.isNotEmpty(course)){
            course.setInventCode(investCode);
            return AjaxResult.success(course);
        }else {
            return AjaxResult.error(5005,"未查询到数据！");
        }
    }


    @ApiOperation(value = "创建一门课程")
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody Course course){
        boolean save = courseService.createCourse(course);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "解散一门课程")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable String id){
        boolean remove = courseService.deleteCourseById(id);
        if (remove){
            redisUtils.del(Constants.COURSE_KEY + id);
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}

